/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp;

import utils.Email;
import edu.chl.niclun.projectwebshop.nbp.Entities.Client;
import edu.chl.niclun.projectwebshop.nbp.Entities.Movie;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**
 *Controller bean of pay.xhtml
 * @author niclas
 */
@ManagedBean
@RequestScoped
public class PayControllerBean {

    @ManagedProperty(value="#{payBackingBean}")
    private PayBackingBean payBean;
    
    @ManagedProperty(value="#{CartBean}")
    CartModelBean cartModelBean;
    
    @ManagedProperty(value="#{LoginBean}")
    private LoginBean loginBean;
    
    /** Creates a new instance of ViewCartControllerBean */
    public PayControllerBean() {
    }
    
    //updates the pay view
    @PostConstruct
    public void update() {
        payBean.setCart(cartModelBean.getCart());
        payBean.setTotalCost(cartModelBean.getTotalCost());
        payBean.setShippingCost(cartModelBean.getShippingCost());
        payBean.setTotCostIncShipping(cartModelBean.getTotCostIncShipping());
        payBean.setMovieList(cartModelBean.getMovieList());
    }
    
    public PayBackingBean getPayBean() {
        return payBean;
    }
    
    public void setPayBean(PayBackingBean payBean) {
        this.payBean = payBean;
    }
    
    public CartModelBean getCartModelBean() {
        return cartModelBean;
    }
    
    public void setCartModelBean(CartModelBean cartModelBean) {
        this.cartModelBean = cartModelBean;
    }
    
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }
    
    public LoginBean getLoginBean() {
        return loginBean;
    }
    //called when we confirmed the purchase.
    public void confirmPurchase() {
        sendMail();
        emptyCart();
    }
    
    //send email
    private void sendMail() {
        if(loginBean.isIsLoggedIn()) {
            Client user = loginBean.getUser();
            String[] email = {user.getEmail() };
            try {
                Email.sendTo(email,"Thank you for your order " + user.getFname() + "!\n\n" + cartModelBean.toString(),
                        "Order confirmation");
            } catch (Exception ex) {
                Logger.getLogger(PayControllerBean.class.getName()).log(Level.SEVERE, ex.toString());
            }
        }
    }
    //navigate
    public String navigateToConfirmation() {
        return "confirmationMsg";
    }
    //add a product
    public void addProduct(Movie m) {
        cartModelBean.addToCart(m);
        update();
    }
    //remove product
    public void removeProduct(Movie m) {
        cartModelBean.removeFromCart(m);
        update();
    }
    //emty cart
    public void emptyCart() {
        cartModelBean.emptyCart();
        update();
    }
}